package com.team.auth.config.security.handler;

import com.alibaba.fastjson.JSON;
import com.team.auth.config.security.SsoProperites;
import com.team.auth.config.security.enums.ResponseStatusEnum;
import com.team.common.base.DataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Service;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Service
public class CustomizeAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    @Autowired
    private SsoProperites ssoProperites;
    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        DataResult dataResult = new DataResult(ResponseStatusEnum.LOGIN_SUCCESS.getCode(), ResponseStatusEnum.LOGIN_SUCCESS.getMsg());
        httpServletResponse.setCharacterEncoding(ssoProperites.getCharacterEncoding());
        httpServletResponse.setStatus(HttpServletResponse.SC_OK);
        httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);
        PrintWriter writer = httpServletResponse.getWriter();
        writer.write(JSON.toJSONString(dataResult));
        writer.flush();
        writer.close();
    }
}
